package com.eyet.comeon.protocol;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.eyet.comeon.bean.Game;
import com.eyet.comeon.data.Data;
import com.eyet.comeon.net.Net;

public class Protocol {

	public boolean Pop_addProtocol(int gid){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("gid" , gid+""));
		String result = Net.sendHttpRequest("comeon.php" , params);
		if(result == null){
			return false;
		}
		Data.popular = Integer.valueOf(result);
		return true;
	}
	
	public boolean Game_listProtocol(){
		String result = Net.sendHttpRequest("list.php", null);
		if(result == null){
			return false;
		}
		try {
			if(Data.game_list == null){
				Data.game_list = new ArrayList<Game>();
			}else{
				Data.game_list.clear();
			}
			JSONArray json = new JSONArray(result);
			for(int i=0; i<json.length() ; i++){
				Game game = new Game();
				JSONObject json2 = json.optJSONObject(i);
				game.gid = json2.getString("gid");
				game.gname = json2.getString("gname");
				game.bz = json2.getString("bz");
				Data.game_list.add(game);
			}
			
			return true;
			
		} catch (JSONException e) {
			Log.e("json exception" , e.getMessage());
		}
		return false;
	}
	
	public boolean Game_addProtocol(String gname , String bz){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("gname" , gname));
		params.add(new BasicNameValuePair("bz" , bz));
		String result = Net.sendHttpRequest("add.php", params);
		if(result == null){
			return false;
		}
		return true;
	}
}
